<template>
    <div class="wrapper">
        <header>
            <p>我的</p>
        </header>

        <div style="height: 7vw;"></div>

        <!-- 用户信息部分 -->
        <li @click="login" class="user-info">
            <!-- 点击头像可以触发上传功能 -->
            <img v-if="avatarUrl" :src="avatarUrl" alt="用户头像" class="avatar" @click="uploadAvatar" />
            <img v-else src="../assets/alipay.png" alt="默认头像" class="avatar" @click="uploadAvatar" />
            <input type="file" ref="fileInput" @change="onFileChange" accept="image/*" style="display: none;" />
            <div class="user-details">
                <p class="username">
                    <template v-if="user">
                        {{ user.userName }}
                    </template>
                    <template v-else>
                        未登录
                    </template>
                </p>
                <p class="phonenumber">
                    <template v-if="user">
                        {{ user.userId }}
                    </template>
                    <template v-else>
                        请登录使用完整功能!
                    </template>
                </p>
            </div>
        </li>
        <div style="height: 5vw;"></div>
        <!-- 用户功能部分 -->
        <ul class="user-functions">
            <li @click="toSetting">
                <i class="fa fa-cog"></i>
                <p>设置</p>
            </li>
            <li @click="toUserAddress">
                <i class="fa fa-map-marker"></i>
                <p>地址管理</p>
            </li>
            <li @click="">
                <i class="fa fa-question-circle"></i>
                <p>帮助</p>
            </li>
            <li @click="logout">
                <i class="fa fa-sign-out"></i>
                <p>退出</p>
            </li>
        </ul>
        
        <!-- 底部菜单部分 -->
        <Footer></Footer>
    </div>
</template>

<script>
//导入共通组件
import Footer from '../components/Footer.vue';

import { ref } from 'vue';

const user = ref(null); // 或者你可以从 props、store 或其他来源初始化 user

export default {
    name: 'MyPage',
    data() {
        return {
            user: {},
            avatarUrl: ''//默认头像
        }
    },
    created() {
        this.user = this.$getSessionStorage('user');
        this.avatarUrl = this.$getLocalStorage('avatarUrl');
    },
    methods: {
        navigateTo(url) {
            // 导航到指定页面
            window.location.href = url;
        },
        logout() {
            // 实现退出登录逻辑，例如清除用户数据并重定向到登录页面
            if (this.user != null) {
                if (confirm('是否要退出')) {
                    this.$removeSessionStorage('user');
                    //this.$refs.customAlert.showAlert("账户已退出！");
                    this.navigateTo('/login');
                }
            }
        },
        login() {
            if (this.user == null) {
                this.navigateTo('/login');
            }
        },
        uploadAvatar() {
            this.$refs.fileInput.click();
        },
        onFileChange(event) {
            const file = event.target.files[0]; // 获取上传的文件
            if (file) {
                const reader = new FileReader();
                // 当文件读取完成时，将其转换为URL
                reader.onload = (e) => {
                    this.avatarUrl = e.target.result; // 更新头像URL
                    this.$setLocalStorage('avatarUrl', this.avatarUrl);
                };
                // 读取文件为Data URL
                reader.readAsDataURL(file);
            }
        },
        toUserAddress() {
            this.navigateTo('/userAddress');
        },
        toSetting() {
            this.navigateTo('/setting')
        }

    },
    components: {
        Footer,//底部菜单事件
    }
}
</script>

<style scoped>
/* General styles */
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

.wrapper {
    padding: 10px;
}

header {
    width: 100%;
    height: 12vw;
    background-color: #0097FF;
    color: #fff;
    font-size: 4.8vw;

    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;

    display: flex;
    justify-content: center;
    align-items: center;
}

/* User info styles */
.user-info {
    display: flex;
    align-items: center;
    margin-top: 20px;
    padding: 10px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    text-decoration: none;
    color: inherit;
}

.user-info .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    margin-right: 10px;
}

.user-info .user-details {
    flex: 1;
}

.user-info .username {
    font-size: 18px;
    font-weight: bold;
}

.user-info .email {
    font-size: 14px;
    color: #666;
}

/* User functions styles */
.user-functions {
    list-style: none;
    padding: 0;
    margin: 20px 0;
}

.user-functions li {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    background-color: #ffffff;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    margin-bottom: 15px;
    cursor: pointer;
    transition: background-color 0.3s, box-shadow 0.3s;
}

.user-functions li:hover {
    background-color: #f5f5f5;
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

.user-functions li i {
    font-size: 24px;
    color: #007bff;
    margin-right: 15px;
}

.user-functions li p {
    margin: 0;
    font-size: 16px;
    line-height: 1.5;
    flex: 1;
}

/********** 底部菜单部分 *********/
</style>